package com.leyou.item.web;

import com.leyou.exception.LyException;
import com.leyou.item.entity.Item;
import com.leyou.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;


@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @PostMapping
    public Item saveItem(Item item){
        // 判断价格是否为空
        if(item.getPrice() == null){
            throw new LyException(400, "价格不能为空！");
        }
        if(item.getName() == null){
            throw new LyException(400, "名称不能为空！");
        }

        // 随机生成id，模拟数据库的新增
        int id = new Random().nextInt(100);
        item.setId(id);
        // 返回新增之后的对象，回显id
        return item;
    }
}